package com.rxt.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.rxt.common.result.Result;
import com.rxt.model.domain.QAnswer;
import com.rxt.model.domain.QQuestion;
import com.rxt.mapper.QQuestionMapper;
import com.rxt.model.dto.QuestionDto;
import com.rxt.model.vo.QQuestionVo;
import com.rxt.service.IQAnswerService;
import com.rxt.service.IQQuestionService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.rxt.util.BaseContext;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.stream.Collectors;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author 周梓文
 * @since 2023-08-24
 */
@Service
public class QQuestionServiceImpl extends ServiceImpl<QQuestionMapper, QQuestion> implements IQQuestionService {

    @Autowired
    private QQuestionMapper qQuestionMapper;

    @Autowired
    private IQAnswerService iqAnswerService;

    @Override
    public Result publish(QQuestionVo qQuestionVO) {

        QQuestion qQuestion = new QQuestion();

        qQuestion.setUserId(BaseContext.get().getId());
        qQuestion.setUsername(BaseContext.get().getUsername());

        qQuestion.setExpertId(qQuestionVO.getExpertId());
        qQuestion.setExpert(qQuestionVO.getExpert());

        qQuestion.setContent(qQuestionVO.getContent());

        qQuestion.setPhone(qQuestionVO.getPhone());

        qQuestion.setPlant(qQuestionVO.getPlant());

        qQuestion.setAnswerNum(0);

        this.save(qQuestion);

        return Result.ok(qQuestion);

    }

    @Override
    public Result getByExpertId(Long expertId) {

        LambdaQueryWrapper<QQuestion> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(QQuestion::getExpertId,expertId)
                .orderByDesc(QQuestion::getStatus)
                .orderByDesc(QQuestion::getCreateTime);

        List<QQuestion> list = this.list(lambdaQueryWrapper);

        List<QuestionDto> collect = list.stream().map((item) -> {
            Integer status = item.getStatus();
            QuestionDto questionDto = new QuestionDto();
            BeanUtils.copyProperties(item, questionDto);
            if (status == 0) {
                return questionDto;
            }
            QAnswer answer = iqAnswerService.getOne(new LambdaQueryWrapper<QAnswer>().eq(QAnswer::getQuestionId, item.getId()));
            questionDto.setAnswer(answer);
            return questionDto;

        }).collect(Collectors.toList());

        return Result.ok(collect);
    }

    @Override
    public int updateStatusTo_1_ById(Long questionId) {

        qQuestionMapper.updateStatusTo_1_ById(questionId);

        return 1;
    }

    @Override
    public Result hot() {

        LambdaQueryWrapper<QQuestion> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(QQuestion::getStatus, 1);

        Page<QQuestion> page = new Page<>(1,5);
        this.page(page,lambdaQueryWrapper);

        List<QuestionDto> collect = page.getRecords().stream().map((item) -> {

            Long QuestionId = item.getId();
            QAnswer answer = iqAnswerService.getOne(new LambdaQueryWrapper<QAnswer>().eq(QAnswer::getQuestionId, QuestionId));

            QuestionDto questionDto = new QuestionDto();
            BeanUtils.copyProperties(item, questionDto);
            questionDto.setAnswer(answer);

            return questionDto;
        }).collect(Collectors.toList());

        return Result.ok(collect);
    }

    @Override
    public Result getUsers() {

        Long userId = BaseContext.get().getId();

        List<QuestionDto> collect = this.list(new LambdaQueryWrapper<QQuestion>().eq(QQuestion::getUserId, userId)).stream().map((item) -> {
            QuestionDto questionDto = new QuestionDto();
            BeanUtils.copyProperties(item, questionDto);

            Long QuestionId = item.getId();
            QAnswer answer = iqAnswerService.getOne(new LambdaQueryWrapper<QAnswer>().eq(QAnswer::getQuestionId, QuestionId));
            questionDto.setAnswer(answer);

            return questionDto;
        }).collect(Collectors.toList());

        return Result.ok(collect);
    }

    @Override
    public Result deleteById(Long id) {

        boolean b = this.removeById(id);

        return Result.ok("删除成功");
    }


}
